package com.guigusuqi.hy.controller;

import com.guigusuqi.app.entity.LoginWxUser;
import com.guigusuqi.app.entity.WxUser;
import com.guigusuqi.app.utils.UserThreadLocal;
import com.guigusuqi.commonutils.utils.GetTokenByAuthorizationUtils;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.dto.DeputyQuery;
import com.guigusuqi.hy.entity.DeputyCard;
import com.guigusuqi.hy.service.DeputyCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 副卡管理 前端控制器
 * </p>
 *
 * @author suqi
 * @since 2022-04-13
 */
@RestController
@Api(tags = {"wx-副卡管理"})
@RequestMapping("/hy/deputy-card")
public class DeputyCardController
{
    @Resource
    private DeputyCardService deputyCardService;

    @Resource
    private RedisCache redisCache;
//    @ApiOperation("新增副卡")
//    @PostMapping("addDeputyCard")
//    public Result addDeputyCard(@RequestBody DeputyCard deputyCard)
//    {
//        return deputyCardService.addDeputyCard(deputyCard);
//    }
//
//    @ApiOperation("修改副卡")
//    @PostMapping("updateDeputyCard")
//    public Result updateDeputyCard(@RequestBody DeputyCard deputyCard)
//    {
//        return deputyCardService.updateDeputyCard(deputyCard);
//    }

    @ApiOperation("多条件分页查询副卡")
    @PostMapping("pageDeputyCondition/{current}/{limit}")
    public Result pageDeputyCondition(@PathVariable Long current,
                                      @PathVariable Long limit,
                                      @ApiParam("查询活动多条件组合")
                                      @RequestBody(required = false) DeputyQuery deputyQuery)
    {
        return deputyCardService.pageDeputyCondition(current,limit,deputyQuery);
    }

    @ApiOperation("用户绑定副卡")
    @GetMapping("bindDeputyCard/{deputyCardId}")
    public Result bindDeputyCardByDeputyCardId(@PathVariable(value = "deputyCardId",required = false)String deputyCardId)
    {
        return deputyCardService.bindDeputyCardByDeputyCardId(deputyCardId);
    }

//    @ApiOperation("分享副卡")
//    @GetMapping("/shareUserCard")
    public Result shareUserCard() throws IOException
    {
        //判断该用户是否绑定副卡
        Result result = selectDeputyCard();
        if(result.getMessage().contains("该用户没有绑定副卡！"))
        {
            return Result.success().message("该用户没有绑定副卡！");
        }
        Map<String, Object> data = result.getData();
        List<DeputyCard> deputyCardList = (List<DeputyCard>) data.get("rows");

        return null;
    }

    @ApiOperation("查看用户绑定的副卡信息")
    @GetMapping("/selectDeputyCard")
    public Result selectDeputyCard()
    {
        WxUser wxUser = UserThreadLocal.get();

        String openid = wxUser.getOpenid();

        List<DeputyCard> deputyCardList = deputyCardService.selectDeputyCard(openid);
        if(deputyCardList.size() == 0)
        {
            return Result.success().message("该用户没有绑定副卡！");
        }
        return Result.success().data("rows",deputyCardList);
    }
}

